package com.zys.zys_mybatis;

import com.zys.zys_mybatis.utils.LocalDruidUtil;
import com.zys.zys_mybatis.utils.StringUtil;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: ZYSMybatis
 * @BelongsPackage: com.zys.zys_mybatis
 * @Author: ZYS
 * @CreateTime: 2024-05-29  19:11
 * @Description: TODO
 */
public class ResultMappingHandler {

    private final ResultSet resultSet;
    private final Class<?> returnType;
    private final Method method;

    public ResultMappingHandler(ResultSet resultSet, Class<?> returnType, Method method) {
        this.resultSet = resultSet;
        this.returnType = returnType;
        this.method = method;
    }

    public Object getResult() throws Exception {
        //如果返回对象是List
        if (returnType.equals(List.class)) {
            //获取泛型类型
            Class<?> listReturnType = (Class<?>) ((ParameterizedType) method.getGenericReturnType()).getActualTypeArguments()[0];
            List<Object> returnList = new ArrayList<>();

            //获取获取BeanInfo对象,排除Object父类属性
            BeanInfo beanInfo = Introspector.getBeanInfo(listReturnType, Object.class);

            //获取属性描述符
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

            //遍历
            while (resultSet.next()) {
                Object o1 = listReturnType.newInstance();
                //将resultSet中数据映射到list中一个对象里
                resultSetToPojo(resultSet, o1, propertyDescriptors);
                returnList.add(o1);
            }
            return returnList;
        }
        if (!resultSet.next()) {
            return null;
        }
        //创建返回对象
        Object returnInstance = returnType.newInstance();
        //获取获取BeanInfo对象,排除Object父类属性
        BeanInfo beanInfo = Introspector.getBeanInfo(returnType, Object.class);
        //获取属性描述符
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        //遍历属性描述符
        if (propertyDescriptors == null) {
            return null;
        }
        resultSetToPojo(resultSet, returnInstance, propertyDescriptors);
        return returnInstance;
    }

    private void resultSetToPojo(ResultSet resultSet, Object o1,
                                 PropertyDescriptor[] propertyDescriptors)
            throws SQLException, InvocationTargetException, IllegalAccessException {
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            //获取属性名
            String propertyName = StringUtil.camelToUnderline(propertyDescriptor.getName());
            //获取属性值
            Object propertyValue = resultSet.getObject(propertyName);
            if (propertyValue != null) {
                //将属性值设置到对象中
                propertyDescriptor.getWriteMethod().invoke(o1, propertyValue);
            }
        }
    }
}
